#########################################################################
#
#    This file is part of LWX. 
#
#    LWX is the abbreviation of the Light WiMax simulation program which 
#    is a ns2 WiMAX network simulation module. LWX allows each user to 
#    apply his/her designed scheduling algorithm or other mechanism used 
#    in WiMAX network and generate the corresponding simulation results
#    for further analysis or other non-commercial purposes.
#
#    Copyright (C) 2008 Yen-Hung Cheng (pplong), NTUST in Taiwan, R.O.C.
#    E-Mail: lwx.ns2@gmail.com
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##########################################################################


# calculation each flow's average throughput and rate
# archetype

#############################
#  set and get parameters
#############################

# specify flow 

$_from = "0.0";
$_to   = "2.0";

# open log file
_log_file = File.open(ARGV[0].to_s, "r");

$_log_name   = ARGV[1].to_s;
if( $_log_name == "" ) then
(
  $_log_name = "log" ;
)
end

$_start_time =2;
$_interval   =0.5;
$_start_period = 0;
$_end_period = 9;

# for throughput
$_throughput =0;
$_total_th   =0;

# for delay
$_num_of_receive = 0;
$_time_of_delay = 0;


# store log to another log
$_log = File.open($_log_name,"w");

$_log.puts("time  throughput  rate(bps)");

#############################
#  calculation
#############################

_log_file.each_line  { |line|
  (
    _str = line.split();
    
    if( _str[0] == "r" &&  _str[30] == $_from && _str[32] == $_to) then
    ( 
      # for rate
        if ( _str[2].to_f - $_start_time < $_interval ) then
        (
          $_num_of_receive = $_num_of_receive + 1;
          $_throughput = $_throughput + _str[36].to_f ;
        ) else 
        (
          _rate = $_throughput * 8 / $_interval ;

          $_log.puts( $_start_time.to_s + "  " + $_throughput.to_s + "  " + _rate.to_s + "\n"); 
          
          # re-count for next interval's throughput is zero
          while ( $_start_time + $_interval + $_interval < _str[2].to_f) do
          (
            $_start_time = $_start_time + $_interval ;
            $_log.puts($_start_time.to_s + " 0 0 \n");
          )
          end
          $_num_of_receive = 1 ;
          $_throughput = _str[36].to_f ; 
          $_start_time = $_start_time + $_interval ;        
        )end 
      # for total throughput
      
      $_total_th = $_total_th + _str[36].to_f ;
    )
    end
  )
}

$_log.puts("total throughput: " + $_total_th.to_s + "\n");

$_log.close();
